using Senparc.Weixin.QY.Helpers;
using System;
using System.Text;
using System.Web;
namespace Senparc.Weixin.QY.TenPayLib
{
	public class TenPayUtil
	{
		public static string GetNoncestr()
		{
			Random random = new Random();
			return MD5UtilHelper.GetMD5(random.Next(1000).ToString(), "GBK");
		}
		public static string GetTimestamp()
		{
			return Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds).ToString();
		}
		public static string UrlEncode(string instr, string charset)
		{
			if (instr == null || instr.Trim() == "")
			{
				return "";
			}
			string result;
			try
			{
				result = System.Web.HttpUtility.UrlEncode(instr, Encoding.GetEncoding(charset));
			}
			catch (Exception)
			{
				result = System.Web.HttpUtility.UrlEncode(instr, Encoding.GetEncoding("GB2312"));
			}
			return result;
		}
		public static string UrlDecode(string instr, string charset)
		{
			if (instr == null || instr.Trim() == "")
			{
				return "";
			}
			string result;
			try
			{
				result = System.Web.HttpUtility.UrlDecode(instr, Encoding.GetEncoding(charset));
			}
			catch (Exception)
			{
				result = System.Web.HttpUtility.UrlDecode(instr, Encoding.GetEncoding("GB2312"));
			}
			return result;
		}
		public static uint UnixStamp()
		{
			return Convert.ToUInt32((DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds);
		}
		public static string BuildRandomStr(int length)
		{
			Random random = new Random();
			string text = random.Next().ToString();
			if (text.Length > length)
			{
				text = text.Substring(0, length);
			}
			else
			{
				if (text.Length < length)
				{
					for (int i = length - text.Length; i > 0; i--)
					{
						text.Insert(0, "0");
					}
				}
			}
			return text;
		}
	}
}
